package com.orange.core.service.repairProblemCategory.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.orange.core.mapper.repairProblemCategory.RepairProblemCategoryMapper;
import com.orange.core.pojo.entity.repairProblemCategory.RepairProblemCategory;
import com.orange.core.pojo.entity.repairProblemCategory.dto.RepairProblemCategoryDTO;
import com.orange.core.pojo.entity.repairProblemCategory.vo.RepairProblemCategoryVO;
import com.orange.core.service.repairProblemCategory.RepairProblemCategoryService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author lwm
* @description 针对表【repair_problem_category(报修单问题分类表)】的数据库操作Service实现
* @createDate 2025-06-06 22:11:19
*/
@Service
public class RepairProblemCategoryServiceImpl extends ServiceImpl<RepairProblemCategoryMapper, RepairProblemCategory>
    implements RepairProblemCategoryService {

    @Override
    @Transactional
    public Boolean save(RepairProblemCategoryDTO repairProblemCategoryDTO) {
        boolean exists = lambdaQuery().eq(RepairProblemCategory::getCategoryName, repairProblemCategoryDTO.getCategoryName())
                .exists();
        Assert.isFalse(exists, "分类名称已存在");
        RepairProblemCategory repairProblemCategory = new RepairProblemCategory();
        BeanUtil.copyProperties(repairProblemCategoryDTO,repairProblemCategory);
        boolean result = this.save(repairProblemCategory);
        return result;
    }

    @Override
    public RepairProblemCategoryVO info(Integer id) {
        RepairProblemCategory repairProblemCategory = this.getById(id);
        Assert.notNull(repairProblemCategory, "分类不存在");
        RepairProblemCategoryVO repairProblemCategoryVO = new RepairProblemCategoryVO();
        BeanUtil.copyProperties(repairProblemCategory, repairProblemCategoryVO);
        return repairProblemCategoryVO;
    }

    @Override
    @Transactional
    public Boolean update(RepairProblemCategoryDTO repairProblemCategoryDTO) {
        RepairProblemCategory repairProblemCategory = new RepairProblemCategory();
        BeanUtil.copyProperties(repairProblemCategoryDTO, repairProblemCategory);
        boolean result = this.updateById(repairProblemCategory);
        return result;
    }

    @Override
    public List<RepairProblemCategoryVO> repairProblemCategoryList() {
        List<RepairProblemCategory> categories = lambdaQuery().orderByAsc(RepairProblemCategory::getSort).list();
        List<RepairProblemCategoryVO> problemCategoryVOS = categories.stream().map(repairProblemCategory -> {
            RepairProblemCategoryVO repairProblemCategoryVO = new RepairProblemCategoryVO();
            BeanUtil.copyProperties(repairProblemCategory, repairProblemCategoryVO);
            return repairProblemCategoryVO;
        }).collect(Collectors.toList());
        return problemCategoryVOS;
    }

    @Override
    @Transactional
    public boolean deleteRepairProblemCategoryByIds(Integer[] ids) {
        boolean result = this.removeBatchByIds(Arrays.asList(ids));
        return result;
    }
}




